/*
 *  This file is part of LifeBudget - Copyright 2010 Clifton Eaton
 *  ------------------------------------------------------------------
 *  LifeBudget is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  LifeBudget is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with LifeBudget.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.lifebudget.app.client.mvc.model;

import java.io.Serializable;
import java.util.Date;

import com.extjs.gxt.ui.client.data.BeanModelTag;

/**
 * The Class Transaction holds information regaring Transactions.
 * Transactions can be one of two types, a Paycheck transaction, or a
 * Categorized transaction.  Paycheck transactions describe transactions 
 * that have PaycheckCalculators applied to them, creating multiple categories.
 * Categorized transactions have a single Category associated with them.  
 * At some point, it would be good for this class to support a Categorized Transaction
 * with multiple categories, but it does not at this time.
 * 
 * Serves as model object to the view as well as a Data Transfer Object and 
 * therefore, meant to be used only in the client code or for communicating 
 * to through GWT RPC.
 */
public class Transaction implements Serializable, BeanModelTag {
	
	/**
	 * The Enum TransactionType.
	 */
	public enum TransactionType {
		
		/** The Paycheck. */
		Paycheck, 
		/** The Categorized. */
		Categorized
	}

	/** The id. */
	private long id;
	
	/** The date. */
	private Date date;
	
	/** The description. */
	private String description;
	
	/** The amount. */
	private double amount;
	
	/** The type. */
	private TransactionType type;
	
	/** The name of the associated paycheck calculator or category depending on type. */
	private String associationName;
	
	/** The category. */
	private Category category;
	
	/** The paycheck calculator. */
	private PaycheckCalculator paycheckCalc;

	/**
	 * Instantiates a new transaction.
	 */
	public Transaction() {
		super();
	}

	/**
	 * Gets the date.
	 *
	 * @return the date
	 */
	public Date getDate() {
		return date;
	}

	/**
	 * Sets the date.
	 *
	 * @param date the new date
	 */
	public void setDate(Date date) {
		this.date = date;
	}

	/**
	 * Gets the description.
	 *
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * Sets the description.
	 *
	 * @param description the new description
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * Gets the amount.
	 *
	 * @return the amount
	 */
	public double getAmount() {
		return amount;
	}

	/**
	 * Sets the amount.
	 *
	 * @param amount the new amount
	 */
	public void setAmount(double amount) {
		this.amount = amount;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Transaction date:" + date.toString() + " description:" + description + " amount:" + amount;
	}

	/**
	 * Sets the id.
	 *
	 * @param id the new id
	 */
	public void setId(long id) {
		this.id = id;
	}

	/**
	 * Gets the id.
	 *
	 * @return the id
	 */
	public long getId() {
		return id;
	}

	/**
	 * Sets the type.
	 *
	 * @param type the new type
	 */
	public void setType(TransactionType type) {
		this.type = type;
	}

	/**
	 * Sets the type.
	 *
	 * @param type the new type
	 */
	public void setType(String type) {
		this.type = TransactionType.valueOf(type);
	}

	/**
	 * Gets the type.
	 *
	 * @return the type
	 */
	public TransactionType getType() {
		return type;
	}

	/**
	 * Sets the association name.
	 *
	 * @param associationName the new association name
	 */
	public void setAssociationName(String associationName) {
		this.associationName = associationName;
	}

	/**
	 * Gets the association name.
	 *
	 * @return the association name
	 */
	public String getAssociationName() {
		return associationName;
	}

	/**
	 * Sets the category.
	 *
	 * @param category the new category
	 */
	public void setCategory(Category category) {
		if (category != null) {
			this.associationName = category.getName();
		}
		this.category = category;
	}

	/**
	 * Gets the category.
	 *
	 * @return the category
	 */
	public Category getCategory() {
		return category;
	}

	/**
	 * Sets the paycheck calc.
	 *
	 * @param paycheckCalc the new paycheck calc
	 */
	public void setPaycheckCalc(PaycheckCalculator paycheckCalc) {
		if (paycheckCalc != null) {
			this.associationName = paycheckCalc.getName();
		}

		this.paycheckCalc = paycheckCalc;
	}

	/**
	 * Gets the paycheck calc.
	 *
	 * @return the paycheck calc
	 */
	public PaycheckCalculator getPaycheckCalc() {
		return paycheckCalc;
	}

}
